package thread;

import java.util.*;

/**
 * 常用集合API
 * ArrayList，LinkedList,
 * */
public class SyncApiDemo2 {
    public static void main(String[] args) {
        //List<Integer> c = new ArrayList<>();//不稳定
        //Set<Integer> c = new HashSet<>();//不稳定
        //List<Integer> c = new LinkedList<>();//不稳定

        //将现有的list集合转换为哟呵并发安全的集合
        //List<Integer> c = Collections.synchronizedList(new ArrayList<>());
        //Set<Integer> c = Collections.synchronizedSet(new HashSet<>());
        List<Integer> c = Collections.synchronizedList(new LinkedList<>());



        Thread t1 = new Thread(){
            public void run(){
                for (int i=0;i<1000;i++){
                    c.add(i);
                }
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                for (int i=1000;i<2000;i++){
                    c.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        System.out.println(c.size());
    }
}
